package com.ptt.sqm.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

import android.util.Log;

import com.google.gson.annotations.SerializedName;
import com.ptt.sqm.model.AssessSubject5019;
import com.ptt.sqm.model.AssessItemChoice5018;
import com.ptt.sqm.model.AssessSuggestion5034;
import com.ptt.sqm.model.AssessTran;
import com.ptt.sqm.model.BusinessSupportSQM;
import com.ptt.sqm.model.ContactImage;
import com.ptt.sqm.model.ContactInfo;
import com.ptt.sqm.model.EquipmentSQM;
import com.ptt.sqm.model.SuggestionImage;
import com.ptt.sqm.model.SupportInfo;

public class WebserviceUtil {

	public static SoapObject getContactInfo(String tagName, List<ContactInfo> contacts) {
		SoapObject soapDetails = new SoapObject(null, tagName);
		SoapObject soapDetail[] = new SoapObject[contacts.size()];

		for (int i = 0; i < contacts.size(); i++) {
			ContactInfo contact = contacts.get(i);

			soapDetail[i] = new SoapObject(null, ContactInfo.NAME);

			soapDetail[i].addProperty(AppUtil.RESOURCE_ID, contact.RESOURCE_ID);
			soapDetail[i].addProperty(AppUtil.IDX, contact.Idx);
			soapDetail[i].addProperty(AppUtil.NAME, contact.Name);

			soapDetail[i].addProperty(AppUtil.TEL, contact.Tel);
			soapDetail[i].addProperty(AppUtil.MOBILE, contact.Mobile);
			soapDetail[i].addProperty(AppUtil.FAX, contact.Fax);

			soapDetail[i].addProperty(AppUtil.EMAIL, contact.Email);
			soapDetail[i].addProperty(AppUtil.TRANSACTIONID, contact.TransactionId);
			soapDetail[i].addProperty(AppUtil.ISUPDATE, contact.IsUpdate);
			
			if (contact.Images != null &&contact.Images.size() != 0) {
				List<ContactImage> ContactImageList = contact.Images.get(0).ContactImage;
				
				SoapObject SoapImages = new SoapObject(null, AppUtil.Images);
				SoapObject SoapImage[] = new SoapObject[ContactImageList.size()];
				int imageCount = 0;

				for (ContactImage image : ContactImageList) {
					
					SoapImage[imageCount] = new SoapObject(null, AppUtil.ContactImage);
					SoapImage[imageCount].addProperty(AppUtil.STATION_ID, AppUtil.toString(image.Station_ID));
					SoapImage[imageCount].addProperty(AppUtil.FILE_SAVE_NAME, AppUtil.toString(image.FILE_SAVE_NAME));
					SoapImage[imageCount].addProperty(AppUtil.FILE_ORIGINAL_NAME, AppUtil.toString(image.FILE_ORIGINAL_NAME));
					SoapImage[imageCount].addProperty(AppUtil.FILE_CONTENT_TYPE, AppUtil.toString(image.FILE_CONTENT_TYPE));
					SoapImage[imageCount].addProperty(AppUtil.RESOURCE_TYPENAME, AppUtil.toString(image.RESOURCE_TYPENAME));
					SoapImage[imageCount].addProperty(AppUtil.RESOURCE_DESCRIPTION, AppUtil.toString(image.RESOURCE_DESCRIPTION));
					SoapImage[imageCount].addProperty(AppUtil.FILE_SIZE, AppUtil.toString(image.FILE_SIZE));
					SoapImage[imageCount].addProperty(AppUtil.CreatedDt, AppUtil.toString(image.CreatedDt));
													
					SoapImages.addSoapObject(SoapImage[imageCount]);
					imageCount++;
				}
				
				soapDetail[i].addSoapObject(SoapImages);
			} else {
				soapDetail[i].addProperty(AppUtil.Images, AppUtil.EMPTY);
			}
			
			soapDetails.addSoapObject(soapDetail[i]);

		}
		return soapDetails;
	}

	public static SoapObject getString(String tagName, List<String> string) {
		SoapObject soapDetails = new SoapObject(null, tagName);
		for (int i = 0; i < string.size(); i++) {
			soapDetails.addProperty(AppUtil.STRING, string.get(i));

		}
		return soapDetails;
	}

	public static SoapObject getSupportInfo(String tagName, List<SupportInfo> supports) {
		SoapObject soapDetails = new SoapObject(null, tagName);
		SoapObject soapDetail[] = new SoapObject[supports.size()];

		for (int i = 0; i < supports.size(); i++) {
			SupportInfo support = supports.get(i);

			soapDetail[i] = new SoapObject(null, SupportInfo.NAME);

			soapDetail[i].addProperty(AppUtil.RESOURCE_ID, support.RESOURCE_ID);
			soapDetail[i].addProperty(AppUtil.IDX, support.Idx);
			soapDetail[i].addProperty(AppUtil._TYPE, support._TYPE);

			soapDetail[i].addProperty(AppUtil.SUBTYPE, support.SubType);
			soapDetail[i].addProperty(AppUtil.NUMRECEIVE, support.NumReceive);
			soapDetail[i].addProperty(AppUtil.ACTUALRECEIVE, support.ActaulReceive);

			soapDetail[i].addProperty(AppUtil.REMARK, support.Remark);
			soapDetail[i].addProperty(AppUtil.ISUPDATE, support.IsUpdate);
			soapDetails.addSoapObject(soapDetail[i]);

		}
		return soapDetails;
	}

	public static SoapObject getEquipmentSQM(String tagName, List<EquipmentSQM> equipments) {
		SoapObject soapDetails = new SoapObject(null, tagName);
		SoapObject soapDetail[] = new SoapObject[equipments.size()];

		for (int i = 0; i < equipments.size(); i++) {
			EquipmentSQM equipment = equipments.get(i);

			soapDetail[i] = new SoapObject(null, EquipmentSQM.NAME);

			soapDetail[i].addProperty(AppUtil.RESOURCE_ID, equipment.RESOURCE_ID);
			soapDetail[i].addProperty(AppUtil.IDX_U, equipment.IDX);
			soapDetail[i].addProperty(AppUtil.EQUIPTYPE, equipment.EQUIPTYPE);

			soapDetail[i].addProperty(AppUtil.BRAND, equipment.BRAND);
			soapDetail[i].addProperty(AppUtil.MODEL, equipment.MODEL);
			soapDetail[i].addProperty(AppUtil.SN, equipment.SN);

			soapDetail[i].addProperty(AppUtil.EQUIPCODE, equipment.EQUIPCODE);
			soapDetail[i].addProperty(AppUtil.VALUE, equipment.VALUE);
			soapDetail[i].addProperty(AppUtil.NUM, equipment.NUM);

			soapDetail[i].addProperty(AppUtil.COST, equipment.COST);
			soapDetail[i].addProperty(AppUtil.EQUIPEMENTTYPE, equipment.EQUIPEMENTTYPE);
			soapDetail[i].addProperty(AppUtil.EQUIPEMENTTYPE_ID, equipment.EQUIPEMENTTYPE_ID);
			soapDetail[i].addProperty(AppUtil.ISUPDATE, equipment.IsUpdate);

			soapDetails.addSoapObject(soapDetail[i]);

		}
		return soapDetails;
	}

	public static SoapObject getBusinessSQM(String tagName, List<BusinessSupportSQM> businesses) {
		SoapObject soapDetails = new SoapObject(null, tagName);
		SoapObject soapDetail[] = new SoapObject[businesses.size()];

		for (int i = 0; i < businesses.size(); i++) {
			BusinessSupportSQM business = businesses.get(i);

			soapDetail[i] = new SoapObject(null, BusinessSupportSQM.NAME);

			soapDetail[i].addProperty(AppUtil.RESOURCE_ID, business.RESOURCE_ID);
			soapDetail[i].addProperty(AppUtil.IDX_U, business.IDX);
			soapDetail[i].addProperty(AppUtil.BUSTYPE, business.BUSTYPE);

			soapDetail[i].addProperty(AppUtil.SUPPORTNAME, business.SUPPORTNAME);
			soapDetail[i].addProperty(AppUtil.CUSNAME, business.CUSNAME);
			soapDetail[i].addProperty(AppUtil.STATUS, business.STATUS);

			soapDetail[i].addProperty(AppUtil.ISUPDATE, business.IsUpdate);

			soapDetails.addSoapObject(soapDetail[i]);

		}
		return soapDetails;
	}

	public static SoapObject getAssessTran(String tagName, AssessTran saveAssessTran) {
		// TODO Auto-generated method stub
		SoapObject soapDetails = new SoapObject(null, tagName);

		soapDetails.addProperty(AppUtil.RESOURCE_ID, AppUtil.toString(saveAssessTran.RESOURCE_ID));
		soapDetails.addProperty(AppUtil.YearAssessId, AppUtil.toString(saveAssessTran.YearAssessId));
		soapDetails.addProperty(AppUtil.AssessDate, AppUtil.convertWSDate(saveAssessTran.AssessDate));
		soapDetails.addProperty(AppUtil.StationId, AppUtil.toString(saveAssessTran.StationId));
		soapDetails.addProperty(AppUtil.RemarkThing, AppUtil.toString(saveAssessTran.RemarkThing));
		soapDetails.addProperty(AppUtil.SignatureImageKey, AppUtil.toString(saveAssessTran.SignatureImageKey));
		soapDetails.addProperty(AppUtil.UserId, AppUtil.toString(saveAssessTran.UserId));
		soapDetails.addProperty(AppUtil.UserTypeId, AppUtil.toString(saveAssessTran.UserTypeId));
		soapDetails.addProperty(AppUtil.CycleAssessStartMonth, AppUtil.toString(saveAssessTran.CycleAssessStartMonth));
		soapDetails.addProperty(AppUtil.CycleAssessStartYear, AppUtil.toString(saveAssessTran.CycleAssessStartYear));
		soapDetails.addProperty(AppUtil.CycleAssessEndMonth, AppUtil.toString(saveAssessTran.CycleAssessEndMonth));
		soapDetails.addProperty(AppUtil.CycleAssessEndYear, AppUtil.toString(saveAssessTran.CycleAssessEndYear));
		soapDetails.addProperty(AppUtil.CycleAssessId, AppUtil.toString(saveAssessTran.CycleAssessId));
		soapDetails.addProperty(AppUtil.ProjectId, AppUtil.toString(saveAssessTran.ProjectId));
		soapDetails.addProperty(AppUtil.TotalPoint, AppUtil.toString(saveAssessTran.TotalPoint));
		soapDetails.addProperty(AppUtil.GPSLocation, AppUtil.toString(saveAssessTran.GPSLocation));
		soapDetails.addProperty(AppUtil.OwnerName, AppUtil.toString(saveAssessTran.OwnerName));
		soapDetails.addProperty(AppUtil.OwnerEmail, AppUtil.toString(saveAssessTran.OwnerEmail));
		soapDetails.addProperty(AppUtil.OwnerTel, AppUtil.toString(saveAssessTran.OwnerTel));
		soapDetails.addProperty(AppUtil.PBL_ID, AppUtil.toString(saveAssessTran.PBL_ID));
		soapDetails.addProperty(AppUtil.Status, AppUtil.toString(saveAssessTran.Status));
		soapDetails.addProperty(AppUtil.CreateDate, AppUtil.convertWSDate(saveAssessTran.CreateDate));
//		soapDetails.addProperty(AppUtil.SubmitDate, saveAssessTran.SubmitDate != null ? AppUtil.convertWSDate(saveAssessTran.SubmitDate) : null);
		soapDetails.addProperty(AppUtil.SubmitDate, null);		

		List<String> CommentIds = new ArrayList<String>();
		if (saveAssessTran.Subjects.size() != 0) {
			Collection AessessSubject5019Coll = saveAssessTran.Subjects.get(0).AssessSubject5019Map.values();
			Iterator<AssessSubject5019> AessessSubject5019s = AessessSubject5019Coll.iterator();

			SoapObject SoapSubjects = new SoapObject(null, AppUtil.Subjects);
			SoapObject SoapSubject[] = new SoapObject[AessessSubject5019Coll.size()];
			int subjectCount = 0;

			while (AessessSubject5019s.hasNext()) {
				AssessSubject5019 AssessSubject5019 = AessessSubject5019s.next();
				SoapSubject[subjectCount] = new SoapObject(null, AppUtil.AssessSubject5019);
				SoapSubject[subjectCount].addProperty(AppUtil.AssessSubjectId, AppUtil.toString(AssessSubject5019.AssessSubjectId));
				SoapSubject[subjectCount].addProperty(AppUtil.TotalRawPoint, AppUtil.toString(AssessSubject5019.TotalRawPoint));
				SoapSubject[subjectCount].addProperty(AppUtil.TotalFullPoint, AppUtil.toString(AssessSubject5019.TotalFullPoint));
				SoapSubject[subjectCount].addProperty(AppUtil.WeightPoint, AppUtil.toString(AssessSubject5019.WeightPoint));
				SoapSubject[subjectCount].addProperty(AppUtil.UserComment, AppUtil.toString(AssessSubject5019.UserComment));
				SoapSubject[subjectCount].addProperty(AppUtil.CycleAssessSubjectPoint, AppUtil.toString(AssessSubject5019.CycleAssessSubjectPoint));
				CommentIds.add(AppUtil.toString(AssessSubject5019.CycleAssessSubjectPoint));

				if (AssessSubject5019.Items.size() != 0) {
					Collection AssessItemChoiceColl = AssessSubject5019.Items.get(0).AssessItemChoiceMap.values();
					Iterator<AssessItemChoice5018> AssessItemChoice5018s = AssessItemChoiceColl.iterator();

					SoapObject SoapItems = new SoapObject(null, AppUtil.Items);
					SoapObject SoapItem[] = new SoapObject[AssessItemChoiceColl.size()];
					int itemCount = 0;

					while (AssessItemChoice5018s.hasNext()) {
						AssessItemChoice5018 AssessItemChoice5018 = AssessItemChoice5018s.next();
						SoapItem[itemCount] = new SoapObject(null, AppUtil.AssessItemChoice5018);
						SoapItem[itemCount].addProperty(AppUtil.AssessItemId, AppUtil.toString(AssessItemChoice5018.AssessItemId));
						SoapItem[itemCount].addProperty(AppUtil.AssessSubjectId, AppUtil.toString(AssessItemChoice5018.AssessSubjectId));
						SoapItem[itemCount].addProperty(AppUtil.CycleAssessSubjectId, AppUtil.toString(AssessItemChoice5018.CycleAssessSubjectId));
						SoapItem[itemCount].addProperty(AppUtil.AssessChoiceId, AppUtil.toString(AssessItemChoice5018.AssessChoiceId));
						SoapItem[itemCount].addProperty(AppUtil.AssessChoicePoint, AppUtil.toString(AssessItemChoice5018.AssessChoicePoint));

						if (AssessItemChoice5018.Suggestions != null && AssessItemChoice5018.Suggestions.size() != 0) {
							Collection AssessSuggestion5034Coll = AssessItemChoice5018.Suggestions.get(0).AssessSuggestion5034Map.values();
							Iterator<AssessSuggestion5034> AssessSuggestion5034s = AssessSuggestion5034Coll.iterator();

							SoapObject SoapSuggestions = new SoapObject(null, AppUtil.Suggestions);
							SoapObject SoapSuggestion[] = new SoapObject[AssessSuggestion5034Coll.size()];
							int suggestionCount = 0;

							// Set Property

							while (AssessSuggestion5034s.hasNext()) {
								AssessSuggestion5034 AssessSuggestion5034 = AssessSuggestion5034s.next();
								SoapSuggestion[suggestionCount] = new SoapObject(null, AppUtil.AssessSuggestion5034);
								SoapSuggestion[suggestionCount].addProperty(AppUtil.AssessSuggestionIndex, AppUtil.toString(AssessSuggestion5034.AssessSuggestionIndex));
								SoapSuggestion[suggestionCount].addProperty(AppUtil.AssessSuggestionDesc, AppUtil.toString(AssessSuggestion5034.AssessSuggestionDesc));
								SoapSuggestion[suggestionCount].addProperty(AppUtil.AssessSuggestionDetail, AppUtil.toString(AssessSuggestion5034.AssessSuggestionDetail));
								SoapSuggestion[suggestionCount].addProperty(AppUtil.AssessSuggestionOwner, AppUtil.toString(AssessSuggestion5034.AssessSuggestionOwner));
								SoapSuggestion[suggestionCount].addProperty(AppUtil.AssessSuggestionType, AppUtil.toString(AssessSuggestion5034.AssessSuggestionType));
								SoapSuggestion[suggestionCount].addProperty(AppUtil.AssessSuggestionId, AppUtil.toString(AssessSuggestion5034.AssessSuggestionId));
								SoapSuggestion[suggestionCount].addProperty(AppUtil.RESOURCE_TYPE, AppUtil.toString(AssessSuggestion5034.RESOURCE_TYPE));
								SoapSuggestion[suggestionCount].addProperty(AppUtil.ImageKey, AppUtil.toString(AssessSuggestion5034.ImageKey));
								

								if (AssessSuggestion5034.Images != null && AssessSuggestion5034.Images.size() != 0) {
									List<SuggestionImage> SuggestionImageList = AssessSuggestion5034.Images.get(0).SuggestionImage;
									
									SoapObject SoapImages = new SoapObject(null, AppUtil.Images);
									SoapObject SoapImage[] = new SoapObject[SuggestionImageList.size()];
									int imageCount = 0;

									for (SuggestionImage image : SuggestionImageList) {
										
										SoapImage[imageCount] = new SoapObject(null, AppUtil.SuggestionImage);
										SoapImage[imageCount].addProperty(AppUtil.STATION_ID, AppUtil.toString(image.Station_ID));
										SoapImage[imageCount].addProperty(AppUtil.FILE_SAVE_NAME, AppUtil.toString(image.FILE_SAVE_NAME));
										SoapImage[imageCount].addProperty(AppUtil.FILE_ORIGINAL_NAME, AppUtil.toString(image.FILE_ORIGINAL_NAME));
										SoapImage[imageCount].addProperty(AppUtil.FILE_CONTENT_TYPE, AppUtil.toString(image.FILE_CONTENT_TYPE));
										SoapImage[imageCount].addProperty(AppUtil.RESOURCE_TYPENAME, AppUtil.toString(image.RESOURCE_TYPENAME));
										SoapImage[imageCount].addProperty(AppUtil.RESOURCE_DESCRIPTION, AppUtil.toString(image.RESOURCE_DESCRIPTION));
										SoapImage[imageCount].addProperty(AppUtil.FILE_SIZE, AppUtil.toString(image.FILE_SIZE));
										SoapImage[imageCount].addProperty(AppUtil.CreatedDt, AppUtil.toString(image.CreatedDt));
																		
										SoapImages.addSoapObject(SoapImage[imageCount]);
										imageCount++;
									}
									
									SoapSuggestion[suggestionCount].addSoapObject(SoapImages);
								} else {
									SoapSuggestion[suggestionCount].addProperty(AppUtil.Images, AppUtil.EMPTY);
								}
								
								SoapSuggestions.addSoapObject(SoapSuggestion[suggestionCount]);
								suggestionCount++;
							}
							SoapItem[itemCount].addSoapObject(SoapSuggestions);
						} else {
							SoapItem[itemCount].addProperty(AppUtil.Suggestions, AppUtil.EMPTY);
						}

						SoapItems.addSoapObject(SoapItem[itemCount]);						
						itemCount++;
					}					
					SoapSubject[subjectCount].addSoapObject(SoapItems);
				} else {
					SoapSubject[subjectCount].addProperty(AppUtil.Items, AppUtil.EMPTY);
				}

				SoapSubjects.addSoapObject(SoapSubject[subjectCount]);
				subjectCount++;
			}
			soapDetails.addSoapObject(SoapSubjects);
		} else {
			soapDetails.addProperty(AppUtil.Subjects, AppUtil.EMPTY);
		}
		soapDetails.addProperty(AppUtil.CommentIds, AppUtil.EMPTY);
//		soapDetails.addSoapObject(getString(AppUtil.CommentIds, CommentIds));

		return soapDetails;
	}
}
